﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Foothill.WF.Command;
using Foothill.Utils;
using Foothill.WF.Core;
using GGFW.Utils.Constants;
using GGFW.Entity.DocumentsManage;
using GGFW.BusinessLogic.DocumentsManage;

namespace GGFW.Command.DocumentsManage
{
    public class ProductOrderCommand : DispatchCommand
    {
        /// <summary>
        /// 生产单信息业务逻辑访问类
        /// </summary>
        private readonly ProductOrderControl ProductOrderCtrl = new ProductOrderControl();

        # region 获取生产单Grid信息
        /// <summary>
        ///  获取生产单Grid信息
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="requestType">RequestType</param>
        /// <param name="url">url</param>
        /// <returns>返回生产单信息数据</returns>
        public ResultBase onGetProductGrid(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                //将查询条件封装
                productgrid productgridEntity = GetFormEntity<productgrid>();
                result = SearchProductGrid(productgridEntity, context);
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        /// <summary>
        /// 获取查询条件的方法
        /// </summary>
        /// <param name="queryEntity">查询条件</param>
        /// <param name="context">请求</param>
        /// <returns></returns>
        public FoothillPageData<productgrid> SearchProductGrid(productgrid productgridEntity, HttpContext context)
        {
            string start = context.Request["start"];
            string limit = context.Request["limit"];
            productgridEntity.OrderField = context.Request["sort"];
            productgridEntity.OrderDirection = context.Request["dir"];
            int intStart = NumericUtil.toInteger(start, 0);
            int intLimit = NumericUtil.toInteger(limit, 1);
            if (intLimit == 0) intLimit = 1;
            int pageNo = intStart / intLimit + 1;
            int pageSize = intLimit;
            return ProductOrderCtrl.SearchProductGrid(pageSize, pageNo, productgridEntity);
        }
        #endregion

        #region 保存生产单
        /// <summary>
        /// 保存生产单
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onSaveProductOrder(HttpContext context, RequestType requestType, string url)
        {

            //返回列表集合
            ResultBase result = new ResultBase();
            try
            {
                int pro_id = 0;
                string productFileds = context.Request["productFileds"].Replace('\'', '’').Replace('ψ', '\'');
                string productaddFileds = context.Request["productaddFileds"].Replace('\'', '’').Replace('ψ', '\'');
                string productgridFileds = context.Request["productgridFileds"].Replace('\'', '’').Replace('ψ', '\'');
                //string maokuanFileds = context.Request["maokuanFileds"].Replace('\'', '’').Replace('ψ', '\'');
                string baozhuangFileds = context.Request["baozhuangFileds"].Replace('\'', '’').Replace('ψ', '\'');
                if (!string.IsNullOrEmpty(productFileds))
                    pro_id = ProductOrderCtrl.SaveProduct(productFileds);

                if (pro_id != 0)
                {
                    if (!string.IsNullOrEmpty(productaddFileds))
                    {
                        if (ProductOrderCtrl.SaveProductAdd(productaddFileds, pro_id))
                            if (ProductOrderCtrl.SaveProductGrid(productgridFileds, pro_id))
                                //if (ProductOrderCtrl.SaveMaoKuan(maokuanFileds, pro_id))
                                ProductOrderCtrl.SaveBaoZhuang(baozhuangFileds, pro_id);
                    }
                }
                result.Success = true;

            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region 生产单列表
        /// <summary>
        ///  生产单列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onSearchProductOrderList(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                //将查询条件封装
                product productqueryEntity = GetFormEntity<product>();
                result = SearchProductOrderList(productqueryEntity, context);
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 获取查询条件的方法
        /// </summary>
        /// <param name="queryEntity">查询条件</param>
        /// <param name="context">请求</param>
        /// <returns></returns>
        public FoothillPageData<product> SearchProductOrderList(product productqueryEntity, HttpContext context)
        {
            string start = context.Request["start"];
            string limit = context.Request["limit"];
            productqueryEntity.OrderField = context.Request["sort"];
            productqueryEntity.OrderDirection = context.Request["dir"];
            int intStart = NumericUtil.toInteger(start, 0);
            int intLimit = NumericUtil.toInteger(limit, 1);
            if (intLimit == 0) intLimit = 1;
            int pageNo = intStart / intLimit + 1;
            int pageSize = intLimit;

            return ProductOrderCtrl.SearchProductOrderList(pageSize, pageNo, productqueryEntity);
        }
        #endregion

        #region  删除产品信息
        /// <summary>
        /// 删除产品信息
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="requestType">RequestType</param>
        /// <param name="url"></param>
        /// <returns>ResultBase</returns>
        public ResultBase onDelProductByProID(HttpContext context, RequestType requestType, string url)
        {
            //返回结果
            ResultBase result = null;
            try
            {
                //从提交的表单获得数据实体
                string pro_ids = context.Request["pro_ids"].ToString();
                foreach (string pro_id in pro_ids.Split(','))
                {
                    result = ProductOrderCtrl.DelProductByProID(NumericUtil.toInteger(pro_id, 0));
                }
                result.success = true;

            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
            }
            return result;
        }
        #endregion

        #region 获取产品附加信息（修改网页）
        /// <summary>
        /// 加载产品附加信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onLoadProductInfo(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                string pro_id = context.Request["ProID"];
                result = ProductOrderCtrl.LoadProductInfo(pro_id);

                result.Success = true;
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region 更新产品附加信息
        /// <summary>
        /// 更新产品附加信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onUpdateProductInfo(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                string pro_id = context.Request["ProID"];
                string tab_name = context.Request["TabName"];
                string filedname = context.Request["FiledName"];
                string filedvalue = context.Request["FiledValue"];
                result = ProductOrderCtrl.UpdateProductInfo(pro_id, tab_name, filedname, filedvalue);

                result.Success = true;
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region 更新单个产品GRID字段
        /// <summary>
        /// 更新样品单个字段
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onUpdateProductGridfield(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                string pg_id = context.Request["PgID"];
                string pro_id = context.Request["ProID"];
                string filednmae = context.Request["FieldName"];
                string fieldvalue = context.Request["FieldValue"];

                result = ProductOrderCtrl.UpdateProductGridField(pg_id, pro_id, filednmae, fieldvalue);

                result.Success = true;
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region 分页获取产品信息
        /// <summary>
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="requestType">RequestType</param>
        /// <param name="url">url</param>
        /// <returns>返回客户信息</returns>
        public ResultBase onSearchValidProductOrder(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;

            try
            {
                string start = context.Request["start"];
                string limit = context.Request["limit"];
                int intStart = NumericUtil.toInteger(start, 0);
                int intLimit = NumericUtil.toInteger(limit, 1);
                if (intLimit == 0) intLimit = 1;
                int pageNo = intStart / intLimit + 1;
                int pageSize = intLimit;

                string type = context.Request["Type"];
                string queryWord = context.Request["query"];

                result = ProductOrderCtrl.searchProductOrder(pageSize, pageNo, queryWord, type);
                result.Success = true;
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion
    }
}
